﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NET4.DataNET4;
using NET4.DataNET4.Repository;

namespace NET4.Programma
{
  public partial class OverviewSupplier : Form
  {
    private List<Leverancier> selectedSuppliers;
    protected List<Leverancier> SelectedSuppliers
    {
      get
      {
        if (this.selectedSuppliers == null) { this.selectedSuppliers = new List<Leverancier>(); }
        return this.selectedSuppliers;
      }
      set
      {
        this.selectedSuppliers = value;
      }
    }

    public OverviewSupplier()
    {
      InitializeComponent();
    }

    #region Protected methods
    protected void Init()
    {
      this.Init(null);
    }

    protected void Init(Leverancier supplier)
    {
      IEnumerable<Leverancier> suppliers = LeverancierRepository.Instance.ListByKeyword(this.stxtKeyword.Text);

      this.SelectedSuppliers = new List<Leverancier>();

      if (supplier != null)
      {
        this.SelectedSuppliers.Add(supplier);
      }
      else if (suppliers.Count() > 0)
      {
        this.SelectedSuppliers.Add(suppliers.First());
      }

      this.dgvSuppliers.AutoGenerateColumns = false;
      this.dgvSuppliers.DataSource = suppliers;
    }

    protected void Add()
    {
      DetailSupplier detailSupplier = new DetailSupplier();
      detailSupplier.SupplierChanged += new DetailSupplier.SupplierChangedHandler(this.DetailSupplier_SupplierChanged);
      detailSupplier.ShowDialog();
    }

    protected void Edit()
    {
      DetailSupplier detailSupplier = new DetailSupplier();
      detailSupplier.Supplier = this.SelectedSuppliers.First();
      detailSupplier.SupplierChanged += new DetailSupplier.SupplierChangedHandler(this.DetailSupplier_SupplierChanged);
      detailSupplier.ShowDialog();
    }

    protected void Delete()
    {
      if (MessageBox.Show("Weet u zeker dat u de geselecteerde leveranciers wilt verwijderen?", "Vraag", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
      {
        foreach (Leverancier leverancier in this.SelectedSuppliers)
        {
          LeverancierRepository.Instance.Delete(leverancier);
        }

        this.Init();
      }
    }
    #endregion

    #region EventHandlers
    private void OverviewSupplier_Load(object sender, EventArgs e)
    {
      this.Init();
    }

    private void stxtKeyword_KeyUp(object sender, KeyEventArgs e)
    {
      this.Init();
    }

    private void dgvSuppliers_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      this.SelectedSuppliers = new List<Leverancier>();

      foreach (DataGridViewRow selectedRow in this.dgvSuppliers.SelectedRows)
      {
        Int32 id = 0;
        Int32.TryParse(selectedRow.Cells[0].Value.ToString(), out id);

        Leverancier selectedSupplier = LeverancierRepository.Instance.Select(id);

        if (selectedSupplier != null)
        {
          this.SelectedSuppliers.Add(selectedSupplier);
        }
      }

      if (this.SelectedSuppliers.Count == 1)
      {
        this.sbSupplierEdit.Enabled = true;
        this.sbSupplierDelete.Enabled = true;
      }
      else if (this.SelectedSuppliers.Count >= 1)
      {
        this.sbSupplierEdit.Enabled = false;
        this.sbSupplierDelete.Enabled = true;
      }
      else
      {
        this.sbSupplierEdit.Enabled = false;
        this.sbSupplierDelete.Enabled = false;
      }
    }

    private void sbSupplierAdd_Click(object sender, EventArgs e)
    {
      this.Add();
    }

    private void sbSupplierEdit_Click(object sender, EventArgs e)
    {
      this.Edit();
    }

    private void sbSupplierDelete_Click(object sender, EventArgs e)
    {
      this.Delete();
    }

    private void DetailSupplier_SupplierChanged(object sender, SupplierEventArgs e)
    {
      this.Init(e.Leverancier);
    }
    #endregion
  }
}
